<?php

class Canhan_m extends CI_Model {
   	
	function register_services($data){
		$this->db->insert_batch('canhan_dichvu', $data);
		return $this->db->insert_id();
	}	
	
	function get_newest_pg_tiec($data = array()){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'cn.ngay_cap_nhat',
			'order'		=>	'DESC',
			'paged'		=>	0
		);
		$def = array_merge($def, $data);		
		$this->db->select('u.email,d.slug,d.ten ten_dvu,cn.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('canhan cn', 'cn.user_id=u.id');
		$this->db->join('canhan_dichvu cd', 'cd.canhan_id = cn.id');
		$this->db->join('dichvu d', 'cd.dichvu_id = d.id');
		$this->db->where('u.trang_thai', 1);
		$this->db->where('cn.trang_thai', 1);
		$this->db->where('d.slug','pg-tiec');
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		$this->db->order_by('cn.ngay_duyet', 'DESC');
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);				
		return $this->db->get()->result();
	}
	
	function get_newest_pg_tour($data = array()){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'cn.ngay_cap_nhat',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'not_in'		=>	array()
		);
		$def = array_merge($def, $data);
		$this->db->select('u.email,d.slug,d.ten ten_dvu,cn.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('canhan cn', 'cn.user_id=u.id');
		$this->db->join('canhan_dichvu cd', 'cd.canhan_id = cn.id');
		$this->db->join('dichvu d', 'cd.dichvu_id = d.id');
		$this->db->where('u.trang_thai', 1);
		$this->db->where('cn.trang_thai', 1);
		$this->db->where('d.slug','pg-tour');		
		$this->db->where("cn.id NOT IN (select canhan_id FROM `canhan_dichvu` 
			JOIN `dichvu` ON `dichvu_id` = `id` 
			AND slug IN('pg-tiec'))", null, false);
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('cn.ngay_duyet', 'ASC');
		return $this->db->get()->result();
	}
	
	function get_newest_hoso($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'cn.ngay_cap_nhat',
			'order'		=>	'DESC',
			'paged'		=>	0
		);
		$def = array_merge($def, $data);
		$this->db->select('u.email,cn.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('canhan cn', 'cn.user_id=u.id');
		$this->db->join('canhan_dichvu cd', 'cd.canhan_id = cn.id');
		$this->db->join('dichvu d', 'cd.dichvu_id = d.id');
		$this->db->where('u.trang_thai', 1);
		$this->db->where('cn.trang_thai', 1);
		$this->db->where("cn.id NOT IN (select canhan_id FROM `canhan_dichvu` 
			JOIN `dichvu` ON `dichvu_id` = `id` 
			AND slug IN('pg-tiec','pg-tour'))", null, false);
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('cn.ngay_duyet', 'ASC');
		return $this->db->get()->result();
	}
	
	function get_hoso($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'cn.ngay_cap_nhat',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'trang_thai'=>	-1,
			'ngay_cap_nhat'	=>	false,
			'ho_ten'		=> false,
			'email'		=>	false
		);
		$def = array_merge($def, $data);
		$this->db->select('u.email,cn.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('canhan cn', 'cn.user_id=u.id');
		$this->db->join('canhan_dichvu cd', 'cd.canhan_id = cn.id', 'left');
		$this->db->join('dichvu d', 'cd.dichvu_id = d.id', 'left');
		$this->db->where('u.trang_thai', 1);
		if($def['trang_thai'] >= 0)
			$this->db->where_in('cn.trang_thai', $def['trang_thai']);
		if($def['ho_ten'])
			$this->db->where("cn.ho_ten LIKE '%{$def['ho_ten']}%'", false,false);
		if($def['email'])
			$this->db->where('u.email', $def['email']);
		if($def['ngay_cap_nhat']){
			$this->db->where('cn.ngay_cap_nhat <', $def['ngay_cap_nhat']);
			return $this->db->get()->row();
		}
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);		
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);				
		$this->db->order_by('cn.ngay_duyet', 'DESC');
		return 
		$this->db->get()->result();
		echo $this->db->last_query();die;
	}
	
	function get_newest_pg_by_slug($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'cn.ngay_cap_nhat',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'slug'		=>	'pg-tiec'
		);
		$def = array_merge($def, $data);		
		$this->db->select('u.email,d.slug,d.ten ten_dvu,cn.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('canhan cn', 'cn.user_id=u.id');
		$this->db->join('canhan_dichvu cd', 'cd.canhan_id = cn.id');
		$this->db->join('dichvu d', 'cd.dichvu_id = d.id');
		$this->db->where('u.trang_thai', 1);
		$this->db->where('cn.trang_thai', 1);
		$this->db->where('d.slug',$def['slug']);
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		$this->db->order_by('cn.ngay_duyet', 'DESC');
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);				
		return $this->db->get()->result();
	}
	
	function get_current_canhan(){
		$userdata = $this->session->userdata('userdata');
		if($userdata){
			$this->db->select('cn.*');
			$this->db->from('canhan cn');
			$this->db->where('cn.user_id', $userdata['id']);
			return $this->db->get()->row();
		}
		return false;
	}
	
	function get_canhan($id, $is_admin = false){
		$userdata = $this->session->userdata('userdata');
		$this->db->select('cn.*,t.ten ten_tinh, q.ten ten_quan,u.email,u.trang_thai active');
		$this->db->from('canhan cn');
		$this->db->join('tinh_thanh t', 't.id=cn.tinh_thanh_id','left');
		$this->db->join('quan_huyen q', 'q.id=cn.quan_huyen_id','left');
		$this->db->join('users u', 'u.id=cn.user_id');
		$this->db->where('cn.id', $id);
		if($is_admin)
			return $this->db->get()->row();
		if(!$userdata || $userdata['canhan_id'] != $id)
			$this->db->where('cn.trang_thai', 1);
		return 
		$this->db->get()->row();
		echo $this->db->last_query();
	}
	
	function get_canhan_email($id){
		$this->db->select('cn.*,u.email');
		$this->db->from('canhan cn');
		$this->db->join('users u', 'u.id=cn.user_id');
		$this->db->where('cn.id', $id);
		return 
		$this->db->get()->row();
		echo $this->db->last_query();
	}
	
	function get_canhan_khac($id){
		$this->db->select('cn.*');
		$this->db->from('canhan cn');
		$this->db->where('cn.id <>', $id);
		$this->db->where('cn.trang_thai', 1);
		$this->db->limit(16);
		$this->db->order_by('id','random');
		return 
		$this->db->get()->result();
		echo $this->db->last_query();die;
	}	
	
	function cap_nhat_trang_thai($data){
		return 
		$this->db->update_batch('canhan', $data, 'id');
		echo $this->db->last_query();die;
	}
	
	function search($fillter, $is_count = false){
		$def = array(
			'limit'				=>	12,
			'order_by'			=>	'cn.ngay_cap_nhat',
			'order'				=>	'DESC',
			'paged'				=>	0,
			'dich_vu'			=>	false,
			'tinh_thanh'		=>	false,
			'quan_huyen'		=>	false,
			'chieu_cao_m'		=> 1,
			'tgian_tu'			=>	'',
			'tgian_den'			=>	'',
			'chieu_cao_cm_min'=> false,
			'chieu_cao_cm_max'=> false,
			'gioi_tinh'			=>	false,
			'key'					=>	false
		);
		$def = array_merge($def, $fillter);		
		$this->db->select('u.email,cn.*');
		$this->db->distinct();
		$this->db->from('users u');
		$this->db->join('canhan cn', 'cn.user_id=u.id');
		$this->db->join('canhan_dichvu cd', 'cd.canhan_id = cn.id', 'left');
		$this->db->join('dichvu d', 'cd.dichvu_id = d.id', 'left');
		$this->db->where('u.trang_thai', 1);
		$this->db->where('cn.trang_thai', 1);
		if($def['dich_vu'])
			$this->db->where_in('d.id', $def['dich_vu']);
		if($def['tinh_thanh'])
			$this->db->where('cn.tinh_thanh_id', $def['tinh_thanh']);
		if($def['quan_huyen'])
			$this->db->where('cn.quan_huyen_id', $def['quan_huyen']);
		if($def['tgian_tu'])
			$this->db->where("cn.tgian_tu <= CAST('{$def['tgian_tu']}' as time)", false,false);
		if($def['tgian_den'])
			$this->db->where("cn.tgian_den >= CAST('{$def['tgian_den']}' as time)", false,false);
		if($def['chieu_cao_cm_min'])
			$this->db->where('cn.chieu_cao_cm >=', $def['chieu_cao_cm_min']);
		if($def['chieu_cao_cm_max'])
			$this->db->where('cn.chieu_cao_cm <=', $def['chieu_cao_cm_max']);
		if($def['gioi_tinh'])
			$this->db->where('cn.gioi_tinh', $def['gioi_tinh']);
		if($def['key']){
			// $this->db->or_where('cn.ho_ten LIKE ', '%'.$def['key'].'%');
			// $this->db->or_where('cn.hoc_van LIKE ', '%'.$def['key'].'%');
			// $this->db->or_where('cn.ngoai_ngu LIKE ', '%'.$def['key'].'%');
			// $this->db->or_where('cn.nang_khieu LIKE ', '%'.$def['key'].'%');
			$sql = "(cn.ho_ten LIKE '%{$def['key']}%' ";
			$sql .= "OR cn.hoc_van LIKE '%{$def['key']}%' ";
			$sql .= "OR cn.ngoai_ngu LIKE '%{$def['key']}%' ";
			$sql .= "OR cn.nang_khieu LIKE '%{$def['key']}%' )";
			$this->db->where($sql, null, '1=1');
		}
		if($is_count)
			return count($this->db->get()->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('cn.ngay_duyet', 'ASC');
		return 
		$this->db->get()->result();
		echo $this->db->last_query();die;
	}
	
}
